package com.example.backblog.controller.front.book;

import com.example.backblog.controller.base.BaseController;
import com.example.backblog.dto.book.BookDto;
import com.example.backblog.entity.book.Book;
import com.example.backblog.repository.book.BookRepository;
import com.example.backblog.vo.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
@RequestMapping("/book")
public class BookController extends BaseController<Book, BookDto,BookRepository> {
    @Autowired
    private BookRepository bookRepository;
    @Autowired
    @Override
    public void setBaseRepository(BookRepository baseRepository) {
        this.baseRepository = baseRepository;
    }
    @GetMapping("/list/page")
    public ApiResult bookList(Integer page,Integer size){
        PageRequest pageRequest = PageRequest.of(page, size);
        Page<Book> all = this.baseRepository.findAll(pageRequest);
        return ApiResult.success(all);
    }
    @GetMapping("/one")
    public ApiResult booInfo(Long id){
        Optional<Book> byId = this.baseRepository.findById(id);
        return ApiResult.success(byId.get());
    }
}
